package io.renren.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.blog.entity.BlogCategoryEntity;
import io.renren.modules.blog.dao.BlogCategoryDao;
import io.renren.modules.blog.dto.BlogCategoryDto;
import io.renren.modules.blog.service.BlogCategoryService;
import org.springframework.stereotype.Service;
import io.renren.common.page.PageData;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.transaction.annotation.Transactional;

/**
 * (BlogCategory)表服务实现类
 *
 * @author chy
 * @since 2024-05-30 11:14:40
 */
@AllArgsConstructor
@Service("blogCategoryService")
public class BlogCategoryServiceImpl extends BaseServiceImpl<BlogCategoryDao, BlogCategoryEntity> implements BlogCategoryService {


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BlogCategoryDto get(Long id) {
        return ConvertUtils.sourceToTarget(baseDao.selectById(id), BlogCategoryDto.class);
    }

    /**
     * 分页查询
     *
     * @param params 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData<BlogCategoryDto> page(Map<String, Object> params) {
        IPage<BlogCategoryEntity> page = baseDao.selectPage(
                getPage(params, "sort", false),
                getWrapper(params)
        );
        return getPageData(page, BlogCategoryDto.class);
    }

    private QueryWrapper<BlogCategoryEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param blogCategoryDto 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(BlogCategoryDto blogCategoryDto) {
        BlogCategoryEntity entity = ConvertUtils.sourceToTarget(blogCategoryDto, BlogCategoryEntity.class);
        //保存角色
        insert(entity);
    }

    /**
     * 修改数据
     *
     * @param blogCategoryDto 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BlogCategoryDto blogCategoryDto) {
        BlogCategoryEntity entity = ConvertUtils.sourceToTarget(blogCategoryDto, BlogCategoryEntity.class);
        //更新角色
        updateById(entity);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除角色
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<BlogCategoryDto> getList(Map<String, Object> params) {
        String name = (String) params.get("name");
        LambdaQueryWrapper<BlogCategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null && !name.trim().isEmpty(), BlogCategoryEntity::getName, name);
        queryWrapper.orderByDesc(BlogCategoryEntity::getSort);
        List<BlogCategoryEntity> blogCategoryEntities = baseDao.selectList(queryWrapper);
        return ConvertUtils.sourceToTarget(blogCategoryEntities, BlogCategoryDto.class);
    }
}
